package org.example.asmr;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.example.asmr.vo.fsList.Body;
import org.example.asmr.vo.fsList.Content;
import org.example.asmr.vo.fsList.Data;
import org.example.asmr.vo.fsList.Res;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Objects;


public class TwoAsmrGay {


    private static final String fsListUrl = "https://two.asmr.gay/api/fs/list";
    private static final String folderPath = "D:\\asmr\\TwoAsmrGay";
    private static final String baseDownLoadPath = "https://two.asmr.gay/d";


    public static void main(String[] args) {

        // 创建File对象
        File folder = new File(folderPath);


        // 判断文件夹是否存在并且是一个目录
        if (!folder.exists()) {
            if (folder.mkdirs()) {
                System.out.println("文件夹不存在。现在创建");
            }
        }


        loopGetPage("/asmr/中文音声/秋水");
    }


    private static void download(String fileUrl, String pathName) {
        File destination = new File(pathName);
        HttpUtil.downloadFile(fileUrl, destination);
        System.out.println("下载成功：" + pathName);
    }


    private static void loopGetPage(String path) {

        System.out.println("路径为：" + path);

        Body body = new Body();
        body.setPath(path);

        HttpResponse execute = HttpRequest.post(fsListUrl).body(JSONObject.toJSONString(body)).execute();

        if (execute.isOk()) {
            String body1 = execute.body();
            Res res = JSONObject.parseObject(body1, new TypeReference<Res>() {
            });
            Data data = res.getData();
            List<Content> content = data.getContent();
            for (Content content1 : content) {

                System.out.println("当前路径：" + body.getPath());

                if (content1.getIs_dir()) {
                    String rootPath2;
                    if (body.getPath().equals("/")) {
                        rootPath2 = body.getPath() + content1.getName();
                    } else {
                        rootPath2 = body.getPath() + "/" + content1.getName();
                    }
                    loopGetPage(rootPath2);
                }

                if (Objects.equals(content1.getType(), 0)) {
                    // 压缩包

                }

                if (Objects.equals(content1.getType(), 3)) {
                    // 音频
                    System.out.println("当前文件是音频");
                    String name = content1.getName();

                    System.out.println("当前路径：" + body.getPath());
                    String sign = content1.getSign();

                    String qian = baseDownLoadPath + body.getPath() + "/" + name;
                    String hou;

                    try {
                        hou = customURLEncode(qian);
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException(e);
                    }

                    hou = hou + "?sign=" + sign;

                    System.out.println("编码前：" + qian);
                    System.out.println("编码后：" + hou);

                    String savePath = folderPath + File.separator + body.getPath().replace("/", File.separator);
                    String downPath = savePath + File.separator + content1.getName();
                    System.out.println("将被下载的路径：" + downPath);
                    download(hou, downPath);

                }

                if (Objects.equals(content1.getType(), 4)) {
                    // txt

                }

//                https://two.asmr.gay/d/asmr/%E4%B8%AD%E6%96%87%E9%9F%B3%E5%A3%B0/35%E5%A4%8F%E5%A4%A9/1%20%E5%AE%BF%E8%88%8DPLAY1%20%E5%9C%A8%E5%AF%9D%E5%AE%A4%E9%87%8C%E8%A2%AB%E5%A5%B3%E7%8E%8B....mp3?sign=pdsPCTQmfwBmZ015-tI7AyDccXEcuNYaUnwiSk89tEw=:1800978128
            }
        }


    }


    // 自定义编码方法，仅对非 '/' 字符进行编码
    public static String customURLEncode(String value) throws UnsupportedEncodingException {
        // 编码整个字符串
        String encoded = URLEncoder.encode(value, "utf-8");

        // 保留 '/' 原样不变
        // 处理编码后的字符串，将 '%2F' 替换为 '/'
        // 需要注意的是 URLEncoder.encode 可能会将其他字符也编码为 '%XX'
        // 在这个处理逻辑中，我们只处理 '/' 的情况
        encoded = encoded.replace("%2F", "/").replace("%3A", ":").replace("+", "%20");

        return encoded;
    }


    private static void download() {

    }

}


